package com.lab.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lab.pojo.Project;
import com.lab.service.ProjectService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
public class ProjectController {
    @Resource
    ProjectService projectService;
    Map<String,Object> result=new HashMap<String,Object>();
    @RequestMapping(value = "/listByPage")
    public Map<String,Object> listByPage(Integer page, Integer rows){
        //设置分页参数
        page=(page-1)*rows;
        PageHelper.offsetPage(page, rows);
        //查询所有数据
        List<Project> list= projectService.allProjects();
        //使用PageInfo封装查询结果
        PageInfo<Project> pageInfo=new PageInfo<Project>(list);
        for(Project project:list){
            if(project.getState().equals("1")){
                project.setState("通过");
            }else if(project.getState().equals("0")){
                project.setState("未通过");
            }else if(project.getState().equals("2")){
                project.setState("审核中");
            }else{
                project.setState("实验完成");
            }
        }
        //从PageInfo对象取出查询结果
        //总记录数
        long total=pageInfo.getTotal();
        //当前页数据列表
        List<Project> projectList=pageInfo.getList();
        result.put("total", total);
        result.put("rows", projectList);
        return result;
    }
    /**
     * 添加和修改
     * @param project
     * @return
     */
    @RequestMapping(value="/saveProject",method = RequestMethod.POST)
    public Map<String,Object> saveProject(Project project){
        try {
            projectService.saveProject(project);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @RequestMapping(value="/findProjectById",method = RequestMethod.GET)
    public Project findDeviceById(Long id) {
        Project project= projectService.findProjectById(id);
        return project;
    }

    /**
     *删除数据
     * @param id
     * @return
     */
    @RequestMapping(value="/deleteProject",method = RequestMethod.DELETE)
    public Map<String,Object> deleteProject(Long[] id){
        try {
            projectService.deleteProject(id);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
    @PostMapping("/saveProjectApply")
    public Map<String,Object> saveProjectApply(Project project){
        /*SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd");// a为am/pm的标记
        Date date = new Date();
        project.setStart_date(sdf.format(date));*/
        try {
            project.setState("2");
            projectService.saveProjectApply(project);
            result.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", e.getMessage());
        }
        return result;
    }
}
